home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / d3d.zip / CURVE3.C < prev    next >
Text File  |  1989-11-23  |  3KB  |  101 lines

  1. /* CURVE3: B-spline curve fitting in three dimensions.  The program
  2.    reads an input file and writes an output file, which are both
  3.    compatible with program D3D, so the following scheme is possible:
  4.  
  5.       D3D --> input file --> CURVE3 --> output file --> D3D
  6.  
  7.    The input file contains lines with point numbers i and three-
  8.    dimensional coordinates of the m points P[i], (i = 1, 2,..., m).
  9.    Any section beginning with the word 'Faces' in the file is 
  10.    ignored. The program writes the three-dimensional coordinates of 
  11.    the k points Q[j], (j = 1, 2, ..., k) in the output file (each
  12.    coordinate triplet preceded by a point number j), where k = (m-3)N-1.
  13.    The value of N is read from the keyboard. (N is the number of intervals 
  14.    between to successive points P[i] and P[i+1].
  15.  
  16.             Point Q[1] is an approximation of P[2];
  17.             Point Q[N+1] is an approximation of P[3];
  18.             Point Q[2N+1] is an approximation of P[4];
  19.                ...
  20.             Point Q[k] is an approximation of P[m-1];
  21. */
  22.  
  23. #include <stdio.h>
  24. #include <alloc.h>
  25. #include <process.h>
  26.  
  27. main()
  28. {
  29.    char infil[30], outfil[30];
  30.    int m=0, k, N, i, j, first=0;
  31.    float *x, *y, *z, X, Y, Z, t, xdum, ydum, zdum,
  32.       xA, xB, xC, xD, yA, yB, yC, yD, zA, zB, zC, zD,
  33.       a0, a1, a2, a3, b0, b1, b2, b3, c0, c1, c2, c3;
  34.    FILE *fpin, *fpout;
  35.  
  36.    printf("Input file:  "); scanf("%s", infil);
  37.    printf("Output file: "); scanf("%s", outfil);
  38.    printf("Enter N, the number of intervals between\n");
  39.    printf("two successive given points: "); scanf("%d", &N);
  40.  
  41.     if((fpin = fopen(infil, "r")) == NULL) {
  42.       printf("File not available");
  43.       exit(1);
  44.    }
  45.  
  46. /* Scan the input file to see how much memory is required */
  47.    while(fscanf(fpin, "%*d %f %f %f", &xdum, &ydum, &zdum) > 0)
  48.       m++;
  49.    fclose(fpin);
  50.  
  51.    x = (float *)malloc((m+1) * sizeof(float));
  52.    y = (float *)malloc((m+1) * sizeof(float));
  53.    z = (float *)malloc((m+1) * sizeof(float));
  54.    if(z == NULL) {
  55.       printf("Not enough memory");
  56.       exit(1);
  57.    }
  58.  
  59.    fpin = fopen(infil, "r");
  60.     for(i=1; i<=m; i++)
  61.       fscanf(fpin, "%*d %f %f %f", x+i, y+i, z+i);
  62.    fclose(fpin);
  63.  
  64.    fpout = fopen(outfil, "w");
  65.    for(i=2; i<m-1; i++) {
  66.  
  67.       xA = x[i-1]; xB = x[i]; xC = x[i+1]; xD = x[i+2];
  68.       yA = y[i-1]; yB = y[i]; yC = y[i+1]; yD = y[i+2];
  69.       zA = z[i-1]; zB = z[i]; zC = z[i+1]; zD = z[i+2];
  70.  
  71.       a3 = (-xA + 3.0*(xB-xC) + xD)/6.0;
  72.       a2 = (xA - 2.0*xB + xC)/2.0;
  73.       a1 = (xC - xA)/2.0;
  74.       a0 = (xA + 4.0*xB + xC)/6.0;
  75.  
  76.       b3 = (-yA + 3.0*(yB-yC) + yD)/6.0;
  77.       b2 = (yA - 2.0*yB + yC)/2.0;
  78.       b1 = (yC - yA)/2.0;
  79.       b0 = (yA + 4.0*yB + yC)/6.0;
  80.  
  81.       c3 = (-zA + 3.0*(zB-zC) + zD)/6.0;
  82.       c2 = (zA - 2.0*zB + zC)/2.0;
  83.       c1 = (zC - zA)/2.0;
  84.       c0 = (zA + 4.0*zB + zC)/6.0;
  85.  
  86.       for(j=first; j<=N; j++) {
  87.          t = (float)j/(float)N;
  88.          X = ((a3*t + a2)*t + a1)*t + a0;
  89.          Y = ((b3*t + b2)*t + b1)*t + b0;
  90.          Z = ((c3*t + c2)*t + c1)*t + c0;
  91.          fprintf(fpout, "%d %f %f %f\n", (i-2)*N +j+1, X, Y, Z);
  92.       }
  93.       first = 1;
  94.    }
  95.    fprintf(fpout, "Faces:\n");
  96.    k = (m-3)*N + 1;
  97.    for(j=1; j<k; j++)
  98.       fprintf(fpout, "%d %d.\n", j, j+1);
  99.    fclose(fpout);
  100. }
  101.